--盛情烊溢
local shengqingyangyi = fk.CreateSkill {
    name = "mcdh_shengqingyangyi",
    on_acquire = function(self, player)
        local room = player.room
        local logic = room.logic
        local current = room.current
        if current.phase == Player.Play then
            local rednum = 0
            local blanum = 0
            logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
                for _, move in ipairs(e.data) do
                    if move.toArea == Card.DiscardPile then
                        for _, info in ipairs(move.moveInfo) do
                            local card = Fk:getCardById(info.cardId)
                            if card.color == Card.Red then
                                rednum = rednum + 1
                            elseif card.color == Card.Black then
                                blanum = blanum + 1
                            end
                        end
                    end
                end
            end, Player.HistoryPhase)
            room:setPlayerMark(player, "#mcdh_shengqingyangyi_num-phase",{rednum,blanum})
            room:setPlayerMark(player, "@#mcdh_shengqingyangyi_card-phase", "<font color='red'>"..tostring(rednum).."</font>".."/".."<font color='black'>"..tostring(blanum).."</font>")
        end
    end
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

shengqingyangyi:addEffect(fk.EventPhaseEnd,{
    can_trigger = function(self, event, target, player, data)
        if player:hasSkill(self) and target.phase == Player.Play then
            local num = 0
            player.room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function(e)
                for _, move in ipairs(e.data) do
                    if move.toArea == Card.DiscardPile then
                        for _, info in ipairs(move.moveInfo) do
                            local card = Fk:getCardById(info.cardId)
                            if card.color == Card.Red then
                                num = num + 1
                            elseif card.color == Card.Black then
                                num = num - 1
                            end
                        end
                    end
                end
            end, Player.HistoryPhase)
            return num >= 0
        end
    end,
    on_cost = function (self, event, target, player, data)
        local room = player.room
        local check,use = room:askForUseViewAsSkill(player,"mcdh_shengqingyangyi_viewas","#mcdh_shengqingyangyi_askfor",true,{bypass_times = true})
        if check then
            self.cost_data = use
            return true
        end
    end,
    on_use = function (self, event, target, player, data)
        local room = player.room
        local dat = self.cost_data
        local c = Fk:cloneCard("fire__slash")
        c.skillName = self.name
        c:addSubcards(dat.cards)
        room:useCard {
            from = player.id,
            tos = table.map(dat.targets, function(id) return { id } end),
            card = c,
        }
    end
})

shengqingyangyi:addEffect(fk.AfterCardsMove,{
    can_refresh = function(self, event, target, player, data)
        local room = player.room
        local current = room.current
        if player:hasSkill(self, true) and current.phase == Player.Play then
                for _, move in ipairs(data) do
                    if move.toArea == Card.DiscardPile then
                        return true
                    end
                end
        end
    end,
    on_refresh = function(self, event, target, player, data)
        local room = player.room
        local current = room.current
        local rednum = 0
        local blanum = 0
        local mark = player:getMark("#mcdh_shengqingyangyi_num-phase")
        if type(mark) == "table" then
            rednum = mark[1]
            blanum = mark[2]
        end
        for _, move in ipairs(data) do
            if move.toArea == Card.DiscardPile then
                for _, info in ipairs(move.moveInfo) do
                    local card = Fk:getCardById(info.cardId)
                    if card.color == Card.Red then
                        rednum = rednum + 1
                    elseif card.color == Card.Black then
                        blanum = blanum + 1
                    end
                end
            end
        end
        room:setPlayerMark(player, "#mcdh_shengqingyangyi_num-phase",{rednum,blanum})
        room:setPlayerMark(player, "@#mcdh_shengqingyangyi_card-phase", "<font color='red'>"..tostring(rednum).."</font>".."/"..tostring(blanum))
    end,
})

Fk:loadTranslationTable {
    ["mcdh_shengqingyangyi"] = "盛情",
    [":mcdh_shengqingyangyi"] = "『盛情烊溢』<br>一名角色的出牌阶段结束时，若此阶段置入过弃牌堆的红色牌不少于黑色牌，你可以将一张牌当火【杀】使用。",
    ["#mcdh_shengqingyangyi_askfor"] = "盛情烊溢：你可以将一张牌当火【杀】使用",
    ["@#mcdh_shengqingyangyi_card-phase"] = "盛情 颜色"
}

return shengqingyangyi